<template>
  <div class="default-main">
    
    <button @click="getAllProcess" >获取进程</button>
    <button @click="geterror" >执行失败</button>
    <button @click="getsuccess" >执行成功</button>
    <button @click="testnomethod" >测试不存在的方法</button>
    <button @click="getdata" >测试data值收发</button>
    <button @click="systemerr" >测试系统错误</button>
    <button @click="initProcess" >测试event监听绑定</button>
    <button @click="addpermission" >添加表permission</button>
    <button @click="updatepermission" >更新表permission</button>
    <button @click="get_config" >测试获取配置信息</button>
    <button @click="set_config" >setconfig</button>
    <el-input v-model="content" placeholder="Please input" show-word-limit type="textarea"/>
    <p>{{ greetMsg }}</p>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { invoke } from "@tauri-apps/api/tauri";
import { ElLoading } from 'element-plus';
import { listen } from '@tauri-apps/api/event'
import { doInvoke } from '../utils/invokeapi';

const greetMsg = ref("");


const content = ref("")
const loading = ref(false);
const openLoading = () => {
  loading.value = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
}
const closeLoading = () => {
  loading.value.close()
}
async function systemerr() {
  greetMsg.value = await doInvoke(
      "return_sys_err",
      {},
      {loading:true},
      {
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)'
      }
    );
}

async function set_config() {
  let jsonDate = JSON.parse(content.value);
  greetMsg.value = await doInvoke(
      "set_config",
      jsonDate,
      {loading:true},
      {
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)'
      }
    );
}

async function get_config() {
  greetMsg.value = await doInvoke(
      "get_config",
      {},
      {loading:true},
      {
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)'
      }
    );
}

async function getAllProcess() {
  openLoading()
  // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
  greetMsg.value = await invoke("docom",{  method:"get_sys_info",data: "" });
  closeLoading()
}
async function geterror() {
  openLoading()
  greetMsg.value = await invoke("docom",{  method:"error",data: "" });
  closeLoading()
}
async function getsuccess() {
  openLoading()
  greetMsg.value = await invoke("docom",{  method:"success",data: "" });
  closeLoading()
}
async function testnomethod() {
  openLoading()
  greetMsg.value = await invoke("docom",{  method:"no",data: "测试方法畅通" });
  closeLoading()
}
async function getdata() {
  openLoading()
  greetMsg.value = await invoke("docom",{  method:"return_post_data",data: "测试方法畅通" });
  closeLoading()
}
async function addpermission() {
  openLoading()
  greetMsg.value = await invoke("do_fn_orm",{  method:"auth/add_permission",data:content.value });
  closeLoading()
}
async function updatepermission() {
  openLoading()
  //console.log(content.value)
  //console.log(JSON.stringify(content.value))
  greetMsg.value = await invoke("do_fn_orm",{  method:"auth/update_permission",data:content.value });
  closeLoading()
}
async function initProcess() {
  openLoading()
  await invoke("init_process");
  await listen("event-name", (event) => {
    greetMsg.value = event.payload.message;
  });

  closeLoading()
}
</script>